﻿package com.boyaa.shells.tool 
{
	import com.boyaa.shells.control.ButtonManager;
	import com.boyaa.shells.control.GlobalVar;
	import com.boyaa.shells.control.LayerManager;
	import com.boyaa.shells.GameConfig;
	import com.boyaa.shells.lang.LangManager;
	import com.boyaa.shells.view.GetProp;
	import com.boyaa.shells.view.ScreenManager;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;

	
	/**
	 * 提示框基類
	 * @author fly
	 */
	public class TooltipBase 
	{
		protected var _cancelBtn:MovieClip = null;
		protected var _okBtn:MovieClip = null;
		protected var _closeBtn:MovieClip = null;
		protected var _bg:MovieClip = null;
		protected var _displayObject:MovieClip = null;
		protected var _showingWay:int = 0;
		
		public function TooltipBase(mc:MovieClip) 
		{
			setUI(mc)
		}
		
		public function setUI(mc:MovieClip):void
		{
			if (mc == null) {
				return;
			}
			_bg = GetProp.getResourceByName("ViewBG");
			_displayObject = mc;
			_closeBtn = mc.closeBtn;
			_okBtn = mc.okBtn;
			_cancelBtn = mc.cancelBtn;
			_displayObject.x = GameConfig.DIMENSIONS.x / 2;
			_displayObject.y = GameConfig.DIMENSIONS.y / 2;
			MCUtil.StopDisplayList(mc);
		}
		
		/**
		 *  显示
		 */
		public function show():void
		{
			LayerManager.instance.addToLayer(LayerManager.TIPS_LAYER, _bg);
			LayerManager.instance.addToLayer(LayerManager.TIPS_LAYER, _displayObject);
			if (_showingWay == 0)
				GameTween.instance.elasticTween(_displayObject);
			addEvent();
		}
		
		/**
		 *  关闭
		 */
		public function hide():void
		{
			remove();
		}
		
		/**
		 *  加事件
		 */
		protected function addEvent():void
		{
			_cancelBtn && ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _cancelBtn, onCancel);
			_okBtn && ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _okBtn, onOK);
			_closeBtn && ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _closeBtn, onCancel);
			//_bg && ButtonManager.instance.addButton(ButtonManager.TOOLTIP, _bg, onCancel);
			// add by Yokia 
			//GlobalVar.instance.getMain().stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
		}
		
		/**
		 * 键盘侦听事件
		 * 
		 * by yokia
		 * @param	e
		 */
		private function keyDown(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.ENTER && (_okBtn && _okBtn.currentFrame == 1))
				onOK(null);
			if (e.keyCode == Keyboard.ESCAPE && _closeBtn)
				remove();
		}
		
		/**
		 *  删事件
		 */
		protected function remove():void
		{
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _cancelBtn);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _okBtn);
			ButtonManager.instance.removeButton(ButtonManager.TOOLTIP, _closeBtn);
			//GlobalVar.instance.getMain().stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			_bg.parent && _bg.parent.removeChild(_bg);
			_displayObject.parent && _displayObject.parent.removeChild(_displayObject);
		}
		
		/**
		 *  退出
		 * @param	e
		 */
		protected function onCancel(e:MouseEvent):void
		{
			remove();
		}
		
		/**
		 *  确定
		 * @param	e
		 */
		protected function onOK(e:MouseEvent):void
		{
			remove();
		}
	}
}